﻿/*******************************************************************************
 * Copyright (c) 2010-2011 Robert Fraser                                       *
 *                                                                             *
 * Permission is hereby granted, free of charge, to any person obtaining a     *
 * copy of this software and associated documentation files (the "Software"),  *
 * to deal in the Software without restriction, including without limitation   *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
 * and/or sell copies of the Software, and to permit persons to whom the       *
 * Software is furnished to do so, subject to the following conditions:        *
 *                                                                             *
 *   - The above copyright notice and this permission notice shall be included *
 *     in all copies or substantial portions of the Software.                  *
 *                                                                             *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
 * DEALINGS IN THE SOFTWARE.                                                   *
 *******************************************************************************/

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace zeal.ui.util
{
    internal static class UiUtil
    {
        public static readonly Size INFINITE_SIZE = new Size(Double.PositiveInfinity, Double.PositiveInfinity);

        public static void bindCommand(this UIElement elem, RoutedCommand command, Action action) { elem.CommandBindings.Add(new CommandBinding(command, createExecuteHandler(action))); }
        public static void bindCommand(this UIElement elem, RoutedCommand command, Func<bool> func, Action action) { elem.CommandBindings.Add(new CommandBinding(command, createExecuteHandler(action), createCanExecuteHandler(func))); }
        private static CanExecuteRoutedEventHandler createCanExecuteHandler(Func<bool> func) { return (_, args) => args.CanExecute = func(); }
        private static ExecutedRoutedEventHandler createExecuteHandler(Action action) { return (_, args) => action(); }

        public static Color multiplyHsl(this Color color, double satFactor, double litFactor)
        {
            ColorHsl hsl = new ColorHsl(color);
            hsl.sat *= satFactor;
            hsl.lit *= litFactor;
            return hsl.toColor();
        }

        public static T frozen<T>(this T freezable) where T : Freezable
        {
            if(!freezable.IsFrozen)
                freezable.Freeze();
            return freezable;
        }

        public static T getTemplatePart<T>(this Control control, string name) { return (T) control.Template.FindName(name, control); }

        public static void addChild(this Panel p, UIElement e) { p.Children.Add(e); }
        public static void removeChild(this Panel p, UIElement e) { p.Children.Remove(e); }

        public static readonly IEasingFunction quadraticEase = new QuadraticEase().frozen();
        public static readonly IEasingFunction cubicEase = new CubicEase().frozen();
        public static readonly IEasingFunction quarticEase = new QuarticEase().frozen();
        public static readonly IEasingFunction quinticEase = new QuinticEase().frozen();
        public static readonly IEasingFunction sineEase = new SineEase().frozen();
        public static readonly IEasingFunction circleEase = new CircleEase().frozen();

        public  static void animate<T>(this T target, DependencyProperty<double> dp, double to, double duration, IEasingFunction ease)
            where T : DependencyObject, IAnimatable
        {
            DoubleAnimation anim = new DoubleAnimation(dp.GetValue(target), to, new Duration(TimeSpan.FromSeconds(duration)));
            anim.EasingFunction = ease;
            anim.Completed += (_, args) =>
            {
                target.BeginAnimation(dp, null);
                dp.SetValue(target, to);
            };
            anim.Freeze();
            target.BeginAnimation(dp, anim, HandoffBehavior.SnapshotAndReplace);
        }
    }
}